<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="mechanismName">
        <el-select v-model="queryParams.mechanismName"
                   placeholder="请输入机构名称"
                   clearable
                   @keyup.enter.native="handleQuery">
          <el-option
            v-for="item in mechanismList"
            :key="item.id"
            :label="item.mechanism_name"
            :value="item.mechanism_name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="businessName">
        <el-input
          v-model="queryParams.businessName"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入店铺UID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item prop="tel">
        <el-input
          v-model="queryParams.tel"
          placeholder="请输入店铺电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-delete" size="mini" @click="resetQuery">清空</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['work:sellermechanism:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['work:sellermechanism:remove']"
        >批量取消合作</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['work:sellermechanism:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sellermechanismList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="店铺UID" align="center" prop="id" />
      <el-table-column label="店铺名称" align="center" prop="business_name" />
      <el-table-column label="供应商" align="center" prop="mechanism_name" />
      <el-table-column label="所在城市" align="center" prop="province_name" />
      <el-table-column label="区域编号" align="center" prop="addvcd" />
      <el-table-column label="机构编号" align="center" prop="mechanism_code" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['work:sellermechanism:remove']"
          >取消合作</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 建立合作对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="合作商户" prop="sellerId">
          <el-select v-model="form.sellerId"
                     placeholder="请输入合作商户"
                     clearable>
            <el-option
              v-for="item in businessList"
              :key="item.id"
              :label="item.business_name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="新合作机构" prop="mechanismId">
          <el-select v-model="form.mechanismId"
                     placeholder="请输入合作机构"
                     clearable>
            <el-option
              v-for="item in mechanismList"
              :key="item.id"
              :label="item.mechanism_name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button icon="el-icon-close" @click="cancel">取 消</el-button>
        <el-button icon="el-icon-check" type="primary" @click="submitForm">建立合作</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSellermechanism, delSellermechanism, addSellermechanism,getBusinessName,getMechanismName } from "@/api/work/sellermechanism";
let rules = {
  sellerId: [
    {required: true, message: "请选择合作商户", trigger: "blur"},
  ],
  mechanismId: [
    {required: true, message: "请选择合作机构", trigger: "blur"}
  ]
};
export default {
  name: "Sellermechanism",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户关系管理表格数据
      sellermechanismList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mechanismName: null,
        businessName: null,
        id: null,
        tel: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: rules,
      businessList: [],
      mechanismList: [],
    };
  },
  created() {
    this.getList();
    this.getBusinessList();
    this.getMechanismList();
  },
  methods: {
    // 获取机构名称列表
    getMechanismList(){
      getMechanismName().then(response =>{
        this.mechanismList = response.rows;
      })
    },
    // 获取商铺名称列表
    getBusinessList(){
      getBusinessName().then(response => {
        this.businessList = response.rows;
      })
    },
    /** 查询客户关系管理列表 */
    getList() {
      this.loading = true;
      listSellermechanism(this.queryParams).then(response => {
        this.sellermechanismList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sellerId: null,
        mechanismId: null,
        createTime: null,
        isDeleted: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 清空(重置)按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "建立合作";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
            addSellermechanism(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
        }
      });
    },
    /** 取消合作按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认取消编号为"' + ids + '"的合作关系？').then(function() {
        return delSellermechanism(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("取消合作关系成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('work/sellermechanism/export', {
        ...this.queryParams
      }, `sellermechanism_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
